package com.edmond.controller;

import com.edmond.entity.Category;
import com.edmond.entity.Goods;
import com.edmond.service.ICategoryService;
import com.edmond.service.IGoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class FrontHomeController {
    @Resource
    private ICategoryService categoryService;
    @Resource
    private IGoodsService goodsService;

    @RequestMapping({"/","/index"})
    public String index(Model model){
        //  将查询的类别列表 封装在model对象中并返回主页
        List<Category> categoryList = categoryService.list();
        //  随机获取 8 个商品显示在首页
        List<Goods> goodsList = goodsService.findRandLimit(8);
        model.addAttribute("categoryList",categoryList);
        //  将查询到的商品列表封装到model对象中进行数据回传
        model.addAttribute("goodsList",goodsList);
        return "/front/index";
    }
    //  根据分类条件查询
    @RequestMapping("/second")
    public String second(Integer categoryId,Model model){
        List<Category> categoryList = categoryService.list();
        List<Goods> goodsList = goodsService.findByCategory(categoryId);
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("categoryId",categoryId);
        return "/front/second";
    }
}
